package com.service.imp;

import java.util.List;

import com.common.utils.Page;
import com.mapper.CustomerMapper;
import com.pojo.Customer;
import com.service.CustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生管理
 */
@Service("customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
	// 声明DAO属性并注入
	@Autowired
	private CustomerMapper customerMapper;

	// 学生列表
	public Page<Customer> findCustomerList(Integer page, Integer rows,
	                                       String custName, String custSource, String custIndustry,
	                                       String custLevel) {
		// 创建学生对象
		Customer customer = new Customer();
		// 判断学生名称
		if (StringUtils.isNotBlank(custName)) {
			customer.setCust_name(custName);
		}
		// 判断学生信息来源
		if (StringUtils.isNotBlank(custSource)) {
			customer.setCust_source(custSource);
		}
		// 判断学生所属行业
		if (StringUtils.isNotBlank(custIndustry)) {
			customer.setCust_industry(custIndustry);
		}
		// 判断学生级别
		if (StringUtils.isNotBlank(custLevel)) {
			customer.setCust_level(custLevel);
		}
		// 当前页
		customer.setStart((page - 1) * rows);
		// 每页数
		customer.setRows(rows);
		// 查询学生列表
		List<Customer> customers =
				customerMapper.selectCustomerList(customer);
		// 查询学生列表总记录数
		Integer count = customerMapper.selectCustomerListCount(customer);
		// 创建Page返回对象
		Page<Customer> result = new Page<>();
		result.setPage(page);
		result.setRows(customers);
		result.setSize(rows);
		result.setTotal(count);
		return result;
	}

	/**
	 * 创建学生
	 */
	@Override
	public int createCustomer(Customer customer) {
		return customerMapper.createCustomer(customer);
	}

	/**
	 * 通过id查询学生
	 */
	@Override
	public Customer getCustomerById(Integer id) {
		Customer customer = customerMapper.getCustomerById(id);
		return customer;
	}

	/**
	 * 更新学生
	 */
	@Override
	public int updateCustomer(Customer customer) {
		return customerMapper.updateCustomer(customer);
	}

	/**
	 * 删除学生
	 */
	@Override
	public int deleteCustomer(Integer id) {
		return customerMapper.deleteCustomer(id);
	}


}
